package com.example.magic;
//
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.example.entity.*;
import com.example.service.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;

//清洗数据
@Controller
public class QingXiJson {
//    这里要对数据进行入库,所以添加了数据源
    @Autowired
    private static CreationTimeService creationtimeService;
    @Autowired
    private static DaysService daysService;
    @Autowired
    private static IsMobileService isMobileService;
    @Autowired
    private static KwService kwService;
    @Autowired
    private static ScoreService scoreService;
    @Autowired
    private static UserLevelService userlevelService;

    public static void and() throws IOException, InterruptedException, ClassNotFoundException {
        Job job = Job.getInstance();
        job.setJobName("QingXiJson");
        job.setJarByClass(QingXiJson.class);

        job.setMapperClass(doMapper.class);
        // job.setReducerClass(doReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        Path in = new Path("E://123//111.206.227.156");
        Path out = new Path("E://123//111.206.227.156//11");
        FileInputFormat.addInputPath(job, in);
        FileOutputFormat.setOutputPath(job, out);
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }


    public static class doMapper extends Mapper<Object, Text, Text, Text> {
        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String initJsonString = value.toString();

            initJsonString = initJsonString.replace("fetchJSON_comment98(", "");
            initJsonString = initJsonString.replace(");", "");

            JSONObject initJson = JSONObject.parseObject(initJsonString);
            if (!initJsonString.contains("productCommentSummary")
                    && !initJsonString.contains("comments")) {
                return;
            }
            JSONObject myjson = initJson.getJSONObject("ten");

            JSONObject productCommentSummary = myjson
                    .getJSONObject("productCommentSummary");
            String productId = productCommentSummary.get("productId")
                    .toString();
            String commentCount = productCommentSummary.get("commentCount")
                    .toString();
            String goodCount = productCommentSummary.get("goodCount")
                    .toString();
            String generalCount = productCommentSummary.get("generalCount")
                    .toString();
            String poorCount = productCommentSummary.get("poorCount")
                    .toString();
            String goodRateShow = productCommentSummary.get("goodRateShow")
                    .toString();
            String generalRateShow = productCommentSummary.get(
                    "generalRateShow").toString();
            String poorRateShow = productCommentSummary.get("poorRateShow")
                    .toString();
            /* comments 包括十条评论 */
            JSONArray comments = myjson.getJSONArray("comments");
            for (int i = 0; i < comments.size(); i++) {
                JSONObject comment = comments.getJSONObject(i);
                String guid = comment.getString("guid");
                String content = comment.getString("content")
                        .replace('\n', ' ');
                String creationTime = comment.getString("creationTime");
                String score = comment.getString("score");
                String nickname = comment.getString("nickname");
                String userLevelName = comment.getString("userClient");
                String userClientShow = comment.getString("mobileVersion");
                String isMobile = comment.getString("isMobile");
                String days = comment.getString("days");
                StringBuilder sb = new StringBuilder();

                sb.append(productId);// 产品ID
                System.out.println("爬取的第"+i+"条数据的产品ID"+productId);
                sb.append("\t");
                sb.append(commentCount);//评论数
                System.out.println("爬取的第"+i+"条数据的评论数"+commentCount);
                sb.append("\t");
                sb.append(goodCount);// 好评数
                System.out.println("爬取的第"+i+"条数据的好评数"+generalCount);
                sb.append("\t");
                sb.append(generalCount);//中评数
                System.out.println("爬取的第"+i+"条数据的中评数"+generalCount);
                sb.append("\t");
                sb.append(poorCount);//差评数
                System.out.println("爬取的第"+i+"条数据的差评数"+poorCount);
                sb.append("\t");
                sb.append(goodRateShow);//好评率
                System.out.println("爬取的第"+i+"条数据的好评率"+goodRateShow);
                sb.append("\t");
                sb.append(generalRateShow);//中评率
                System.out.println("爬取的第"+i+"条数据的中评率"+generalRateShow);
                sb.append("\t");
                sb.append(poorRateShow);//差评率
                System.out.println("爬取的第"+i+"条数据的差评率"+poorRateShow);
                sb.append("\t");
                sb.append(guid);//随机生成ID
                System.out.println("爬取的第"+i+"条数据的随机生成ID"+guid);
                sb.append("\t");
                sb.append(content);//评论内容
                System.out.println("爬取的第"+i+"条数据的评论内容"+content);
                sb.append("\t");
                sb.append(creationTime);//写评论的时间
                System.out.println("爬取的第"+i+"条数据的写评论的时间"+creationTime);
                sb.append("\t");
                sb.append(score);//打分
                System.out.println("爬取的第"+i+"条数据的打分"+score);
                sb.append("\t");
                sb.append(nickname);//昵称
                System.out.println("爬取的第"+i+"条数据的昵称"+nickname);
                sb.append("\t");
                sb.append(userLevelName);//会员级别
                System.out.println("爬取的第"+i+"条数据的会员级别"+userLevelName);
                sb.append("\t");
                sb.append(userClientShow);//
//                System.out.println("爬取的第"+i+"条数据的"+userClientShow);
                sb.append("\t");
                sb.append(isMobile);//
                System.out.println("爬取的第"+i+"条数据的用户端"+isMobile);
                sb.append("\t");
                sb.append(days);
                System.out.println("爬取的第"+i+"条数据的用户评论天数"+days);

                System.out.println("开始创建实体对象");
                CreationTimeEntity creationTimeEntity = new CreationTimeEntity();
                DaysEntity daysEntity = new DaysEntity();
                IsMobileEntity isMobileEntity = new IsMobileEntity();
                KwEntity kwEntity = new KwEntity();
                ScoreEntity scoreEntity = new ScoreEntity();
                UserLevelEntity userLevelEntity = new UserLevelEntity();
                isMobileEntity.setIsmobile(isMobile);
                kwEntity.setKw(content);
                userLevelEntity.setUserlevelname(userLevelName);

//                creationtimeService.save(creationTimeEntity);
//                daysService.save(daysEntity);
//                isMobileService.save(isMobileEntity);
//                kwService.save(kwEntity);
//                scoreService.save(scoreEntity);
//                userlevelService.save(userLevelEntity);



                String result = sb.toString();
//                context.write(new Text(result), new Text(""));
            }
        }
    }
}
